//
//  MainTabBarController.swift
//  CandyDate
//
//  Created by zolobdz on 2017/9/26.
//  Copyright © 2017年 HeChao. All rights reserved.
//

import UIKit
import AudioToolbox
import Photos
import TZImagePickerController
import Qiniu
import ObjectMapper

class MainTabBarController: BaseTabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        
//        delegate = self
        
//        // 云信消息代理
//        NIMSDKConfig.shared().delegate = self
//        NIMSDK.shared().chatManager.add(self)
//        NIMSDK.shared().conversationManager.add(self)
//
//        // 登录注册
//        NotificationCenter.default.addObserver(self, selector: #selector(userDidSignUp), name: NSNotification.Name.CDUserSignupSuccess, object: nil)
//        NotificationCenter.default.addObserver(self, selector: #selector(userDidLogin), name: NSNotification.Name.CDUserLoginSuccess, object: nil)
//
//        // system
//        NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)
//        NotificationCenter.default.addObserver(self, selector: #selector(aplicationDidEnterBackground), name: NSNotification.Name.UIApplicationDidEnterBackground, object: nil)
        
        judgeLocationService()
        
        
        
        
       
        
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
//        CDIMService.shared.restAudioSessionSetting()

        if let userID = MyselfSingleton.shared.userInfo?.userID,!userID.isEmpty {
        }
        else{
            showLoginViewController()
            return
        }
//        CDPushService.shared.tabbarLaunched()
    }
    
    //MARK: Public
    
    
//    func reloadRedPoint() {
//        messageRedPointView.isHidden = CDIMService.shared.unreadCount() <= 0
//    }
    
    func currentNavigationController() -> UINavigationController? {
        if presentedViewController != nil{
            return nil // present
        }
        if let array = viewControllers as? [UINavigationController]
        {
            return array[selectedIndex]
        }
        return nil
    }
    
    func recoverForPresent() {
        if presentedViewController != nil{
            presentedViewController?.dismiss(animated: false, completion: nil)
        }
    }
    
    //MARK: Custom Notifications
    
    func userDidSignUp() {
        CDUtility.log("注册成功")
    }
    
    func userDidLogin() {
    }
    
    
    //MARK: System Notifications
    
    /* app进入前台目前要做的事情：
     2.上报日活(防止连续两天app没有杀死而不上报日活)
     3.获取appSetting
     4.获取每日活动
     5.展示强制更新(防止点击跳转到appleStore却没有更新，回来后又能够正常使用)
     6.展示数字密码锁
     */
    func applicationDidBecomeActive() {
        
        if isEnteredBackground{ // 经过这个方法判断才是真正退出过后台
            isEnteredBackground = false

            
        }
        

    }
    
    func aplicationDidEnterBackground() {
        isEnteredBackground = true
    }

    
}

extension MainTabBarController{
    
    //MARK: Show-UI
    // 弹出登录页
    func showLoginViewController() {
        MyselfSingleton.shared.logOutcClean()
        if let currentViewController = CDUtility.currentViewController(),currentViewController.presentingViewController == UIManager.shared.tabBarController
        {
            currentViewController.dismiss(animated: false, completion: nil)
        }
        
        let nav = LoginNavigationViewController()
        present(nav, animated: true, completion: nil)
        UserDefaults.standard.set(true, forKey: CDUserDefaultKey.firstOpenApplicationKey)
    }
    
    
    //MARK: NetWork
    
    
    // 获取极光红点未读数（主要是通讯录新的好友和我的邀请）
    func fetchContactsUnreadNotify() {
//        guard let myID = MyselfSingleton.shared.userInfo?.userID,!myID.isEmpty else {
//            return
//        }
//        let api = CDRequestAPI.getContactsNotifyUnreadCount(ShowHudKey: false, userID: myID)
//        CDNetWorkService.requestDataWithTarget(target: api, type: ContactsNotifyUnreadModel.self, successClosure: {[weak self] (obj, code, message) in
//            if let strongSelf = self{
//                if code == 10
//                {
//                    if let countModel = obj as? ContactsNotifyUnreadModel
//                    {
//                        let newFriendsCount = Int(countModel.numberOfNewFriends) ?? 0
//                        let myRquestCount = Int(countModel.numberOfMyRequests) ?? 0
//                        guard newFriendsCount > 0 || myRquestCount > 0 else {
//                            return
//                        }
////                        strongSelf.contactRedPointView.isHidden = false
//                        CDPushService.shared.configUnreadCount(newFriendsCount+myRquestCount)
//                        if let contactsViewController = strongSelf.contactsNavigationController.viewControllers.first as? ContactsViewController
//                        {
//                            let tempModel = contactsViewController.myFriendsModel ?? MyFriendsModel(JSON: [:])
//                            if var model = tempModel
//                            {
//                                model.numberOfNewFriends = countModel.numberOfNewFriends
//                                model.numberOfMyRequests = countModel.numberOfMyRequests
//                                contactsViewController.myFriendsModel = model
//                            }
//                        }
//                    }
//                }
//                else
//                {
//                    CDUtility.log(api.path + message)
//                }
//            }
//        }) { (msg) in
//            CDUtility.log(msg)
//        }
    }
    
    
    //MARK: Task
    
    
    //MARK: 修改头像
    
    // 用户登录后需要获取qiniutoken的方法 读取本地qntoken，如果没有或者是过期了自动获取，自动获取弹hud。6小时获取一次
    
}

